package com.polaris.live.ui.message.fragment.dialog

import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import androidx.annotation.CallSuper
import com.blankj.utilcode.util.ScreenUtils
import com.gyf.immersionbar.ImmersionBar
import com.polaris.live.R
import com.polaris.live.common.ext.dp

/**
 * BottomDialogFragment
 *
 * @author Created by lucas on 2023/10/28 13:47
 */
abstract class BottomDialogFragment : ReusableDialogFragment() {

    @CallSuper
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setStyle()
    }

    @CallSuper
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initImmersionBar()
    }

    @CallSuper
    override fun onStart() {
        super.onStart()

        dialog?.window?.apply {
            setWindowAnimations(getAnimationStyle())

            attributes = attributes.also { params ->
                params.dimAmount = 0f
                params.width = ViewGroup.LayoutParams.MATCH_PARENT
                params.height = getDialogHeight()
                params.gravity = Gravity.BOTTOM
            }
        }
    }

    open fun setStyle() {
        setStyle(STYLE_NO_FRAME, R.style.DialogFragmentBottomDialog)
    }

    open fun getAnimationStyle(): Int {
        return R.style.BottomDialogAnimation
    }

    open fun getDialogHeight(): Int {
        return ScreenUtils.getScreenHeight() - getTopMargin()
    }

    open fun getTopMargin(): Int {
        return 234.dp
    }

    open fun initImmersionBar() {
        ImmersionBar.with(this)
            .transparentBar()
            .navigationBarDarkIcon(true)
            .init()
    }

    override val isReusable: Boolean = false
}